package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.domain.ZzExerciseBook;
import com.zzedu.dto.ZzExerciseBookDTO;
import com.zzedu.mapper.ZzExerciseBookMapper;
import com.zzedu.service.IZzExerciseBookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 作业本信息Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-06-02
 */
@Service
public class ZzExerciseBookServiceImpl extends ServiceImpl<ZzExerciseBookMapper, ZzExerciseBook> implements IZzExerciseBookService {

    @Override
    public LambdaQueryWrapper<ZzExerciseBook> buildQuery(ZzExerciseBookDTO zzExerciseBook) {
        LambdaQueryWrapper<ZzExerciseBook> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(null != zzExerciseBook.getClazzId(), ZzExerciseBook::getClazzId, zzExerciseBook.getClazzId());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getPictureBookId(), ZzExerciseBook::getPictureBookId, zzExerciseBook.getPictureBookId());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getNodeId(), ZzExerciseBook::getNodeId, zzExerciseBook.getNodeId());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getContentId(), ZzExerciseBook::getContentId, zzExerciseBook.getContentId());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getStudentId(), ZzExerciseBook::getStudentId, zzExerciseBook.getStudentId());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getSubmitCount(), ZzExerciseBook::getSubmitCount, zzExerciseBook.getSubmitCount());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getCorrectState(), ZzExerciseBook::getCorrectState, zzExerciseBook.getCorrectState());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getExamScore(), ZzExerciseBook::getExamScore, zzExerciseBook.getExamScore());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getSubmitId(), ZzExerciseBook::getSubmitId, zzExerciseBook.getSubmitId());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getCorrectTeacherId(), ZzExerciseBook::getCorrectTeacherId, zzExerciseBook.getCorrectTeacherId());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getGmtCreate(), ZzExerciseBook::getGmtCreate, zzExerciseBook.getGmtCreate());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getGmtModify(), ZzExerciseBook::getGmtModify, zzExerciseBook.getGmtModify());
        lambdaQueryWrapper.eq(null != zzExerciseBook.getDeleted(), ZzExerciseBook::getDeleted, zzExerciseBook.getDeleted());

        lambdaQueryWrapper.orderByDesc(ZzExerciseBook::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public ZzExerciseBook getByStudentIdAndSubjectId(Long stuId, Long subjectId) {
        return this.getOne(Wrappers.lambdaQuery(ZzExerciseBook.class)
                .eq(ZzExerciseBook::getStudentId, stuId)
                .eq(ZzExerciseBook::getContentId, subjectId).orderByDesc(ZzExerciseBook::getContentId).last("limit 1")
        );
    }

    @Override
    public Long getSubmitCount(Long unitId, Long stuId, String interaction) {
        return baseMapper.getSubmitCount(unitId, stuId, interaction);
    }
    @Override
    public Long getVideoTimes(Long unitId, Long stuId, String subjectType) {
        return baseMapper.getVideoTimes(unitId, stuId, subjectType);
    }

    @Override
    public  List<Map<String, Object>> studentStudyInfoByStudentIdList(List<Long> stuIdList){
        return baseMapper.studentStudyInfoByStudentIdList(stuIdList);
    }
}